package de.bazinga.rotw.menu;

import java.util.Iterator;

import com.jme.image.Texture;
import com.jme.math.Vector3f;
import com.jme.renderer.ColorRGBA;
import com.jme.renderer.Renderer;
import com.jme.scene.Spatial;
import com.jme.scene.Spatial.CullHint;
import com.jme.scene.shape.Quad;
import com.jme.scene.state.TextureState;
import com.jme.util.TextureManager;
import com.jmex.game.state.GameStateManager;

import de.bazinga.rotw.game.Game;
import de.bazinga.rotw.game.RotwGameState;
import de.bazinga.rotw.map.MapList;
import de.bazinga.rotw.menu.item.MenuItem;
import de.bazinga.rotw.network.BasicSyncManager;
import de.bazinga.rotw.network.Client;
import de.bazinga.rotw.network.Server;
import de.bazinga.rotw.network.ServerList;
import de.bazinga.rotw.network.message.MapMessage;
import de.bazinga.rotw.network.message.TeamChangeMessage;
import de.bazinga.rotw.person.Person;

public class IngameMenu extends Menu {
	

	private int currLevel = 0;
	
	
	public IngameMenu() {		
		super("ingameMenu");
	}
	
	
	protected void init() {		
		// Background
		Quad bgQuad = new Quad("bg",
				Game.getGame().getDisplay().getWidth(), 
				Game.getGame().getDisplay().getHeight());
		
		bgQuad.setRenderQueueMode(Renderer.QUEUE_ORTHO);   
	 	
		bgQuad.setLocalTranslation(new Vector3f(
				Game.getGame().getDisplay().getWidth()/2,
				Game.getGame().getDisplay().getHeight()/2,
				0));
		
		TextureState bg = Game.getGame().getDisplay().getRenderer().createTextureState();
		bg.setEnabled(true);
		bg.setTexture(TextureManager.loadTexture(RotwGameState.class
				.getClassLoader().getResource("background/mainMenu.png"),
				Texture.MinificationFilter.BilinearNearestMipMap,
				Texture.MagnificationFilter.Bilinear));
		
		bgQuad.setRenderState(bg);
		
		bgQuad.setLightCombineMode(Spatial.LightCombineMode.Off);
		bgQuad.updateRenderState();
		
		getRootNode().attachChild(bgQuad);
	}
	
	
	protected void initMenuItems() {
		menuItems.put("continue", 
				new MenuItem("continue.png",
						"continue",
						1f,
						Game.getGame().getDisplay().getWidth()/2.0f,
						Game.getGame().getDisplay().getHeight()-150,
						250,100));

		menuItems.put("changeMap", 
				new MenuItem("ingame/changeMap.png",
						"changeMap",
						1f,
						Game.getGame().getDisplay().getWidth()/2.0f,
						Game.getGame().getDisplay().getHeight()-250,
						250,100));
		
		

		
		menuItems.put("prevLevel", 
				new MenuItem("previous.png",
						"prevLevel",
						0.08f,
						200,
						Game.getGame().getDisplay().getHeight()-330,
						64,64));
		
		menuItems.put("level", 
				new MenuItem(MapList.getInstance().getMapList().get(currLevel).get("name"),
						"level",
						ColorRGBA.black,
						2f,
						Game.getGame().getDisplay().getWidth()/3.0f,
						Game.getGame().getDisplay().getHeight()-350));

		otherItems.put("levelMode", 
				new MenuItem("("+MapList.getInstance().getMapList().get(currLevel).get("gameMode")+")",
						"levelMode",
						ColorRGBA.black,
						0.7f,
						Game.getGame().getDisplay().getWidth()/3.0f,
						Game.getGame().getDisplay().getHeight()-360));
		
		
		menuItems.put("nextLevel", 
				new MenuItem("next.png",
						"nextLevel",
						0.08f,
						Game.getGame().getDisplay().getWidth()-200,
						Game.getGame().getDisplay().getHeight()-330,
						64,64));
		
		menuItems.put("team1", 
				new MenuItem("ingame/team1.png",
						"team1",
						1f,
						Game.getGame().getDisplay().getWidth()/2.0f - 150,
						Game.getGame().getDisplay().getHeight() - 450,
						250,100));
		
		menuItems.put("team2", 
				new MenuItem("ingame/team2.png",
						"team2",
						1f,
						Game.getGame().getDisplay().getWidth()/2.0f + 150,
						Game.getGame().getDisplay().getHeight() - 450,
						250,100));
		
		
		menuItems.put("exit", 
				new MenuItem("exit.png",
						"exit",
						2f,
						Game.getGame().getDisplay().getWidth()/2.0f,
						Game.getGame().getDisplay().getHeight()-550,
						250,100));
	}
	
	protected void handleClick(String item) {
		RotwGameState gameState = 
			(RotwGameState)GameStateManager.getInstance().getChild("gameState");
		
		if(menuItems.containsKey(item)) {
			if(item.equals("exit")) {
				if(gameState.isServer()) {
					ServerList.closeServer();
				}
				
				gameState.disconnect();
				GameStateManager.getInstance().getChild("mainMenu").setActive(true);
				this.setActive(false);
				Game.startMenuSound();
			}else if(item.equals("continue")) {
				if(gameState.isRunning()) {
					gameState.setActive(true);
					this.setActive(false);
				}
			}else if(item.equals("changeMap") || item.equals("level")) {
				if(gameState.isServer() && 
						gameState.isRunning()) {
					
					gameState.recreatePhysicsSpace();
					gameState.reset(currLevel,false);
					
					// Send Mapchange
					BasicSyncManager syncManager = gameState.getSyncManager();
					MapMessage mapMsg = new MapMessage();
					mapMsg.setMapName(gameState.getCurrentMapName());
					
					((Server)syncManager).getServer().sendToAll(mapMsg);
					
					gameState.setActive(true);
					this.setActive(false);
				}
			}else if(item.equals("team1") || item.equals("team2")) {
				if(gameState.useTeams()) {	
					// Reset zombie before we switch Teams
					gameState.getPlayer().resetZombie(true);
					
					if(item.equals("team1")) gameState.getPlayer().setTeam(Person.TEAM_ONE);
					else gameState.getPlayer().setTeam(Person.TEAM_TWO);
					
					BasicSyncManager syncManager = gameState.getSyncManager();
					if(syncManager instanceof Client) {
						// Send Message to Server that we switched Teams
						TeamChangeMessage tcm = new TeamChangeMessage();
						if(item.equals("team1")) tcm.setTeam(Person.TEAM_ONE);
						else tcm.setTeam(Person.TEAM_TWO);
						((Client)syncManager).getClient().sendToServer(tcm);
					}else{
						((Server)syncManager).updateScore();
					}
					
					// Reset Player
					gameState.getPlayer().reset(false);
					
					Person.updateAllTeamBoxes();
				}
				gameState.setActive(true);
				this.setActive(false);
			}else if(item.equals("prevLevel")) {
				if(currLevel != 0) {
					currLevel--;
					menuItems.get("level").setText(
							MapList.getInstance().getMapList().get(currLevel).get("name"));
					otherItems.get("levelMode").setText(
							"("+MapList.getInstance().getMapList().get(currLevel).get("gameMode")+")");
				}
			}else if(item.equals("nextLevel")) {
				if(currLevel <  MapList.getInstance().getMapList().size()-1) {
					currLevel++;
					menuItems.get("level").setText(
							MapList.getInstance().getMapList().get(currLevel).get("name"));
					otherItems.get("levelMode").setText(
							"("+MapList.getInstance().getMapList().get(currLevel).get("gameMode")+")");
				}
			}
		}
	}
	
	protected void handleHover(MenuItem hoverItem) {
		Iterator<MenuItem> it = menuItems.values().iterator();
		while(it.hasNext()) {
			MenuItem item = it.next();
			if(hoverItem == item) {
				
				if(item == menuItems.get("prevLevel")) {
					item.setText("previousRed.png");
				}else if(item == menuItems.get("nextLevel")) {
					item.setText("nextRed.png");
				}else if(item == menuItems.get("continue")) {
					item.setText("continueHover.png");
				}else if(item  == menuItems.get("exit")) {
					item.setText("exitHover.png");
				}else if(item  == menuItems.get("team1")) {
					item.setText("ingame/team1Hover.png");
				}else if(item  == menuItems.get("team2")) {
					item.setText("ingame/team2Hover.png");
				}else if(item  == menuItems.get("changeMap")) {
					item.setText("ingame/changeMapHover.png");
				}else{
					item.setTextColor(ColorRGBA.yellow);
				}
			}else{
				if(item == menuItems.get("prevLevel")) {
					item.setText("previous.png");
				}else if(item == menuItems.get("nextLevel")) {
					item.setText("next.png");
				}else if(item == menuItems.get("continue")) {
					item.setText("continue.png");
				}else if(item  == menuItems.get("exit")) {
					item.setText("exit.png");
				}else if(item  == menuItems.get("team1")) {
					item.setText("ingame/team1.png");
				}else if(item  == menuItems.get("team2")) {
					item.setText("ingame/team2.png");
				}else if(item  == menuItems.get("changeMap")) {
					item.setText("ingame/changeMap.png");
				}else{
					item.setTextColor(item.getTextColor());
				}
			}
		}
	}
	
	
	public void setActive(boolean status) {
		super.setActive(status);
		
		if(((RotwGameState)GameStateManager.getInstance().getChild("gameState")).useTeams()) {
			menuItems.get("team1").setCullHint(CullHint.Never);
			menuItems.get("team2").setCullHint(CullHint.Never);
		}else{
			menuItems.get("team1").setCullHint(CullHint.Always);
			menuItems.get("team2").setCullHint(CullHint.Always);
		}
	}
}